package _04文件上传案例;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        System.out.println("服务端启动");
        ServerSocket serverSocket = new ServerSocket(8888);

        while (true){

        Socket socket = serverSocket.accept();
        InputStream socketInputStream = socket.getInputStream();
        OutputStream socketOutputStream = socket.getOutputStream();

        //接收客户端文件
        String fileName = UUID.randomUUID().toString();
        FileOutputStream fos =new FileOutputStream("day12\\服务端文件夹\\bg.jpg");
        byte[] bytes = new byte[1024];
        int read;
        while ((read=socketInputStream.read(bytes))!=-1){
            fos.write(bytes,0,read);
        }
        System.out.println("服务端保存完毕");



        //接收完成。回写确认数据
        socketOutputStream.write("服务端下载完成".getBytes());

        socketOutputStream.close();
        fos.close();
        socketInputStream.close();
        socket.close();
    }
    }
}
